ALTER PROCEDURE [dbo].[CPP_User_ApplyFamily]
  @dwUserID INT,
  @dwKindID INT,
  @szResult NVARCHAR(255) OUTPUT  -- 输出信息
AS
BEGIN
  
  -- 客户端申请XX游戏群主

  DECLARE @NickName NVARCHAR(255) = ''
  SELECT @NickName = NickName FROM RYAccountsDB.dbo.AccountsInfo WHERE AccountsType = 0 AND UserID = @dwUserID
  IF @@ROWCOUNT=0 BEGIN
    SET @szResult = N'玩家[' + CAST(@dwUserID AS NVARCHAR) + ']不存在';
    RETURN 1
  END
  
  -- 小游戏判断
  DECLARE @KindID INT = 0
  DECLARE @KindName NVARCHAR(255) = 'XX游戏'
  SELECT @KindID = ISNULL(KindID, 0), @KindName = KindName FROM RYPlatformDB.dbo.GameKindItem WHERE Nullity = 0 AND KindID = @dwKindID
  IF @@ROWCOUNT<>1 BEGIN
    SET @szResult = N'游戏[' + CAST(@dwKindID AS NVARCHAR) + ']未定义'
    RETURN 2
  END
  
  DECLARE @FamilyID INT = 0
  DECLARE @FamilyCode INT = 0
  DECLARE @State INT = 0
  SELECT @FamilyID = ID, @FamilyCode = Code, @State = [State] FROM [RYWebSMSDB].[dbo].[Family] WHERE AUID = @dwUserID AND KindID = @dwKindID
  IF @@ROWCOUNT<>0 BEGIN
    IF @State=0 BEGIN  -- State: 0=审核中;1=审核通过;
      SET @szResult = N'您的申请正在审核中';
      RETURN 3
    END ELSE IF @State=1 BEGIN
      SET @szResult = N'您已是群[' + CAST(@FamilyCode AS NVARCHAR) + ']的群主,不能拥有多个群';
      RETURN 4
    END ELSE BEGIN
      SET @szResult = N'申请失败,未知状态[' + CAST(@State AS NVARCHAR) + ']'
      RETURN 5
    END
  END

  INSERT INTO [RYWebSMSDB].[dbo].[Family] (
    [AUID], [Caption], [KindID], [State]
  ) VALUES (
    @dwUserID, @KindName + '的群', @dwKindID, 0
  )

  SET @szResult = N'您的申请已提交,请耐心等待';
  RETURN 0
END